package com.hq.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hq.zb.exception.BizException;
import com.hq.zb.exception.BizExceptionEnum;
import com.hq.zb.mapper.DeptMapper;
import com.hq.zb.mapper.RoleMapper;
import com.hq.zb.mapper.RoleMenuMapper;
import com.hq.zb.mapper.RoleUserMapper;
import com.hq.zb.model.*;
import com.hq.zb.service.ICodeRulesHeaderService;
import com.hq.zb.service.IRoleService;
import com.hq.zb.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author huanghanrui
 * @since 2022-03-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ICodeRulesHeaderService codeRulesHeaderService;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Role saveRole(Role role) {
        //角色名称不允许为空
        if (StringUtils.isBlank(role.getRoleName())) {
            throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "角色名称不允许为空.");
        }
        /*if (StringUtils.isBlank(role.getOrgCode())) {
            throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "角色所属组织不允许为空.");
        }*/
        if (StringUtils.isBlank(role.getDeptCode())) {
            throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "角色所属部门不允许为空.");
        } else {
            Wrapper<Dept> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("dept_code", role.getDeptCode());
            List<Dept> depts = deptMapper.selectList(entityWrapper);
            if (CollectionUtil.isEmpty(depts)) {
                throw new BizException(BizExceptionEnum.DATA_NOT_EXIST, "角色所属部门在系统中不存在.");
            } else {
                role.setOrgCode(depts.get(0).getOrgCode());
            }
        }
        role.setEnableFlag(true);
        if (role.getRoleId() == null) {
            roleMapper.insert(role);
        } else {
            roleMapper.updateById(role);
        }
        return role;
    }

    @Override
    public void deleteRole(List<Role> roles) {
        if (CollectionUtil.isNotEmpty(roles)) {
            for (Role role : roles) {
                Wrapper<RoleMenu> wrapper = new EntityWrapper<>();
                wrapper.eq("delete_flag", false);
                wrapper.eq("role_id", role.getRoleId());
                List<RoleMenu> list = roleMenuMapper.selectList(wrapper);
                if (CollectionUtil.isNotEmpty(list)) {
                    throw new BizException(BizExceptionEnum.DATA_ERROR, "该角色已分配菜单权限，不允许删除.");
                }
                Wrapper<RoleUser> roleUserWrapper = new EntityWrapper<>();
                roleUserWrapper.eq("delete_flag", false);
                roleUserWrapper.eq("role_id", role.getRoleId());
                List<RoleUser> roleUsers = roleUserMapper.selectList(roleUserWrapper);
                if (CollectionUtil.isNotEmpty(roleUsers)) {
                    throw new BizException(BizExceptionEnum.DATA_ERROR, "该角色已分配用户，不允许删除.");
                }
                role.setDeleteFlag(true);
                roleMapper.updateById(role);
            }
        }
    }

    @Override
    public Page<RoleVo> queryRolePage(Page<RoleVo> page, Wrapper<RoleVo> wrapper) {
        page.setRecords(roleMapper.queryRolePage(page, wrapper));
        return page;
    }
}
